package com.game.base.server.push;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.bmob.push.BmobPush;
import cn.bmob.v3.BmobInstallation;
import cn.bmob.v3.BmobInstallationManager;
import cn.bmob.v3.BmobPushManager;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.InstallationListener;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.PushListener;

/**
 * 推送服务 @{link http://doc.bmob.cn/push/android/#46}
 */
public class PushManager {
    /**
     * 开启推送服务
     */
    public static void startPush(Context context) {
        BmobInstallationManager.getInstance().initialize(new InstallationListener<BmobInstallation>() {
            @Override
            public void done(BmobInstallation bmobInstallation, BmobException e) {
                if (e == null) {
                    Log.d("bmob_push", "init success" + bmobInstallation.getObjectId() + "-" + bmobInstallation.getInstallationId());
                } else {
                    Log.d("bmob_push", "init error: " + e.getMessage());
                }
            }
        });
        BmobPush.startWork(context);
    }

    /**
     * 客户端推送消息
     */
    public static void pushTextMessage(String text) {
        BmobPushManager bmobPushManager = new BmobPushManager();
        bmobPushManager.pushMessageAll(text, new PushListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.d("bmob_push", "推送成功！");
                } else {
                    Log.d("bmob_push", "异常：" + e.getMessage());
                }
            }
        });
    }

    /**
     * 订阅组推送消息
     */
    public static void pushChannelsMessage() {
        BmobPushManager bmobPushManager1 = new BmobPushManager();
        BmobQuery<BmobInstallation> query = BmobInstallation.getQuery();
        List<String> channels = new ArrayList<>();
        //TODO 替换成你需要推送的所有频道，推送前请确认已有设备订阅了该频道，也就是channels属性存在该值
        channels.add("NBA");
        query.addWhereContainedIn("channels", channels);
        bmobPushManager1.setQuery(query);
        bmobPushManager1.pushMessage("消息内容", new PushListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.d("bmob_push", "推送成功！");
                } else {
                    Log.d("bmob_push", "异常：" + e.getMessage());
                }
            }
        });
    }

    /**
     * 频道订阅/取消订阅等 @{link http://doc.bmob.cn/push/android/#43}
     */
    public static void subscribeChannels() {
        BmobInstallationManager.getInstance().subscribe(Arrays.asList("NBA", "CBA", "IJK", "NBA", "CBA", "USA"), new InstallationListener<BmobInstallation>() {
            @Override
            public void done(BmobInstallation bmobInstallation, BmobException e) {
                if (e == null) {
                    Log.d("bmob_device", "批量订阅成功");
                } else {
                    Log.d("bmob_device", e.getMessage());
                }
            }
        });
    }
}
